package org.firefly.db.excel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.firefly.db.meta.ColumnMeta;
import org.firefly.db.util.DBInfo;

public class ExcelData {
    private DBInfo srcInfo;
    private String tableName;
    private int maxExcelColIndex;//excel最大有效列索引 一定大于或等于title.size
    private Map<Integer, ColumnMeta> title = new HashMap<>();// key:excelTitle,val:tableColumn
    private Map<Integer, String> excelTitles = new HashMap<>();// key:columnIndex,val:excelTitle
    private Map<Integer, Integer> columnLength = new HashMap<>();// key:columnIndex,val:maxLength
    private List<Map<Integer, Object>> data = new ArrayList<>();

    public ExcelData(DBInfo srcInfo, String tableName) {
        this.srcInfo = srcInfo;
        this.tableName = tableName;
    }

    /**
     * 添加列
     * 
     * @author 尹智军
     * @param columnIndex
     * @param excelTitle
     * @param column
     * @param columnType 2016年12月23日
     */
    public void addColumn(int columnIndex, String excelTitle, String column) {
        ColumnMeta cm = new ColumnMeta();
        cm.setColumnName(column);
        
        excelTitles.put(columnIndex, excelTitle);
        title.put(columnIndex, cm);
        if(maxExcelColIndex < columnIndex){
            maxExcelColIndex = columnIndex;
        }
    }
    
    public void setColumnType(int columnIndex, int columnType){
        ColumnMeta cm = title.get(columnIndex);
        cm.setJavaType(columnType);
    }

    /**
     * 设置单元格值
     * 
     * @author 尹智军
     * @param columnIndex
     * @param val 2016年12月23日
     */
    public void setCellVal(int rowIndex, int columnIndex, Object val) {
        Map<Integer, Object> row = null;
        if (data.size() > rowIndex) {
            row = data.get(rowIndex);
        } else {
            while (data.size() <= rowIndex) {
                row = new HashMap<>();
                data.add(row);
            }
        }
        row.put(columnIndex, val);
        if (val != null) {
            int vLength = val.toString().length() * 2;
            if(columnLength.containsKey(columnIndex)){
                int curColumnLength =  columnLength.get(columnIndex);
                if (curColumnLength < vLength) {
                    columnLength.put(columnIndex, vLength);
                }
            }else{
                columnLength.put(columnIndex, vLength);
            }
        }else if(!columnLength.containsKey(columnIndex)){
            columnLength.put(columnIndex, 50);
        }
    }

    public String makeCreateSQL(){
        StringBuilder sql = new StringBuilder("create table ");
        sql.append(tableName).append("( id varchar(50),");
        for (int i = 0; i <= maxExcelColIndex; i++) {
            ColumnMeta t = title.get(i);
            if(t == null)continue;
            int colLength = columnLength.get(i);
            sql.append(t.getColumnName()).append(" ").append(srcInfo.getDialect().javaType2SQLType(t.getJavaType())).append("(").append(colLength).append(")").append(",");
        }
        sql.deleteCharAt(sql.length()-1);
        sql.append(")");
        return sql.toString();
    }

    public String makeInsertTempletSQL() {
        StringBuilder sql = new StringBuilder("insert into ");
        sql.append(tableName).append("( id,");
        for (int i = 0; i <= maxExcelColIndex; i++) {
            ColumnMeta t = title.get(i);
            if(t == null)continue;
            sql.append(t.getColumnName()).append(",");
        }
        sql.deleteCharAt(sql.length()-1);
        sql.append(") values (").append(StringUtils.rightPad("?", (title.size() * 2) + 1, ",?")).append(")");
        return sql.toString();
    }
//    
//    public static void main(String[] args) {
//        Map m = new HashMap<>();
//        Object o = m.get(2);
//       // System.out.println(StringUtils.rightPad("?", 10+1, ",?"));
//    }

    public Object[] makeInsertParams(int rowIndex) {
        Map<Integer, Object> rowData = data.get(rowIndex);
        if(rowData == null)return null;
        Object[] params = new Object[title.size()+1];
        params[0] = UUID.randomUUID().toString();
        int paramIndex = 1;
        for (int i = 0; i <= maxExcelColIndex; i++) {
            ColumnMeta t = title.get(i);
            if(t == null)continue;
            params[paramIndex] = rowData.get(i);
            paramIndex++;
        }
        return params;
    }

    public int getRowCount(){
        return data.size();
    }
}
